## Replication Code
## Accountability for the Local Economy at All Levels of Government in United States Elections
## Justin de Benedictis-Kessner Christopher Warshaw
## To re-assemble data from its separate sources, run files 01, 02, and 03.
## Otherwise, this file can run all analyses on the compiled data
##  and then produce the main tables.


# ------------------- #
#### load packages ####
# ------------------- #

library(foreign)
library(dplyr)
library(arm)
library(lfe)
library(readr)
library(stargazer)

set.seed(6)

#-----------------------------------------#
### Functions to create leads and lags ####
#-----------------------------------------#

## these functions alter the standard lag/lead code to eliminate incorrect lagging/leading when there are missing years
lag.new <- function(x, n = 1L, along_with){
	index <- match(along_with - n, along_with, incomparable = NA)
	out <- x[index]
	attributes(out) <- attributes(x)
	out
}

lead.new <- function(x, n = 1L, along_with){
	index <- match(along_with + n, along_with, incomparable = NA)
	out <- x[index]
	attributes(out) <- attributes(x)
	out
}




#------------------#
####  Load data ####
#------------------#

# data_analysis <- read.dta(file="econ_counties_cities_analysis.dta")
load(file="econ_counties_cities_analysis.Rdata")


#-------------------------------------------#
### Accountability for President’s Party ####
#-------------------------------------------#

federal_basic3 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control | fips+state_year |0| fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_basic3 <- felm(partisanship_state_delta ~ wages_perworker_real_delta* pres_dem_control | fips+state_year |0| fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_basic3b <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control | fips+state_year |0| fips, data=data_analysis[data_analysis$population_2010 > 20000,])

### standardized estimates for introduction
federal_basic3_std <- felm(partisanship_federal_delta ~ scale(wages_perworker_real_delta)* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_basic3b_std <- felm(partisanship_state_delta2 ~ scale(wages_perworker_real_delta)* pres_dem_control |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])


president_basic3 <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_president_basic3 <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
house_president_basic3 <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_president_basic3 <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
sldl_president_basic3 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips,  data=data_analysis[   data_analysis$population_2010 > 20000,])
cities_president_basic3 <- felm(mayor_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
counties_president_basic3 <- felm(countyleg_avg_demshare_delta_alldistricts ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
counties_president_basic32 <- felm(countyleg_avg_demshare_delta_district ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=counties[counties$population_2010 > 20000& counties$n_winners==1,])
stateoffice_basic3 <- felm(stateoffice_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
 
#------------------------------------------------------------------------#
## Table 3: Accountability for President’s Party in Federal Elections ####
#------------------------------------------------------------------------#


stargazer(president_basic3, senate_president_basic3, house_president_basic3, federal_basic3,
          dep.var.caption = "\\emph{Dependent Variable - Change in Vote Share for:}",
					dep.var.labels = c("President", "Senate","House", "Federal Average"),
					omit = "^pres_dem_control",
					order = c("wages_perworker_real_delta:pres_dem_control","wages_perworker_real_delta"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic president",
															 "Change in logged wages"
					),
					float=F,
					add.lines = list(c("FE for State-Year","X","X","X","X"),c("FE for County","X","X","X","X")),
					omit.stat = "ser",
					notes = "Standard errors clustered by county.",notes.align = "l",
					out = "Tables/tab3.tex")

#-----------------------------------------------------------------------#
### Table 4: Accountability for President’s Party in State Elections ####
#-----------------------------------------------------------------------#

stargazer(gov_president_basic3, stateoffice_basic3, sldl_president_basic3, counties_president_basic3, state_basic3b,
          dep.var.caption = "\\emph{Dependent Variable - Change in Vote Share for:}",
          dep.var.labels = c("Governor","Downballot State Offices","State House","County Legislature","State Average"),
					omit = "^pres_dem_control",
					order = c("wages_perworker_real_delta:pres_dem_control","wages_perworker_real_delta"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic president",
															 "Change in logged wages"
					),
					float=F,
					add.lines = list(c("FE for State-Year","X","X","X","X","X"),
													 c("FE for County","X","X","X","X","X")),
					omit.stat = "ser",
					notes = "Standard errors clustered by county.",notes.align = "l",
					out = "Tables/tab4.tex")

# local:
# stargazer(cities_president_basic3, counties_president_basic3, local_basic3)

#------------------------------------------------------#
### Robustness using different model specifications ####
#------------------------------------------------------#

## evaluate importance of FE's
federal_basic3_nofe <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])


#-------------------------------------#
### Robustness using different DVs ####
#-------------------------------------#


### Deltas
## main result
federal1 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## no state-year FE
federal2 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## state-year FE, no county FE
federal3 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## main result, weights for population
federal4 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])
## no state-year FE
federal5 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])


### Levels
## no delta for vote share
federal6 <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## no delta for vote share, but LDV
federal6_LDV <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control +partisanship_federal_lag|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## no delta for vote share + only year+county FE
federal7 <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## no delta for vote share + state-year FE, no county FE
federal8 <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## no delta for vote share, weights for population
federal9 <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])
## no delta for vote share + only year+county FE, weights for population
federal10 <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])

#--------------------------------------------------------------------------------#
### Table 5: Robustness of Results for President’s Party in Federal Elections ####
#--------------------------------------------------------------------------------#

stargazer(federal1,federal2,federal3,federal4,federal5,federal6,federal7,federal8,federal9,federal10,federal6_LDV,
          dep.var.labels = c("$\\Delta$ in Democratic Vote Share","Democratic Vote Share"),
          omit = "^pres_dem_control",
          order = c("wages_perworker_real_delta:pres_dem_control","wages_perworker_real_delta","partisanship_federal_lagged"),
          covariate.labels = c("Change in logged wages $\\times$ Democratic president",
                               "Change in logged wages",
                               "Lagged Democratic voteshare"
          ),
          float=F,
          add.lines = list(c("FE for State-Year",            "X"," ","X","X"," ","X"," ","X","X"," ","X"),
                           c("FE for Year",                  " ","X"," "," ","X"," ","X"," "," ","X"," "),
                           c("FE for County",                "X","X"," ","X","X","X","X"," ","X","X","X"),
                           c("Unweighted, Pop $>$20,000",    "X","X","X"," "," ","X","X","X"," "," ","X"),
                           c("Weighted by Pop, All counties"," "," "," ","X","X"," "," "," ","X","X"," "),
                           " "),
          omit.stat = "ser",notes.append = F,
          notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
          out = "Tables/tab5.tex")


#--------------------------------------------#
### Appendix D: Additional Specifications ####
#--------------------------------------------#

## federal
data_analysis$lagged_dv_common <- data_analysis$partisanship_federal_lag
federal6_LDV <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control +lagged_dv_common|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal6_LDV_nofe <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control +lagged_dv_common|state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## state
data_analysis$lagged_dv_common <- data_analysis$partisanship_state_lag
state6_LDV <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control +lagged_dv_common|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state6_LDV_nofe <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control +lagged_dv_common|state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

stargazer(federal6_LDV,federal6_LDV_nofe,state6_LDV,state6_LDV_nofe,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Vote Share for:}",
					dep.var.labels = c("Federal Average","State Average"),
					omit = "^pres_dem_control",
					order = c("wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta",
										"lagged_dv_common"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic president",
															 "Change in logged wages",
															 "Lagged Democratic voteshare"
					),
					float=F,
					add.lines = list(c("FE for State-Year","X","X","X","X"),
													 c("FE for County","X"," ","X"," ")),
					omit.stat = "ser",
					notes = "Standard errors clustered by county.",notes.align = "l",
					out = "Tables/tabD2.tex")

### Deltas
## main result
state1 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## no state-year FE
state2 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## state-year FE, no county FE
state3 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## main result, weights for population
state4 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])
## no state-year FE
state5 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])


### Levels
## no delta for vote share
state6 <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state6_LDV <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control +partisanship_state_lag|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## no delta for vote share + only year+county FE
state7 <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## no delta for vote share + state-year FE, no county FE
state8 <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## no delta for vote share, weights for population
state9 <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])
## no delta for vote share + only year+county FE, weights for population
state10 <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[!is.na(data_analysis$population),], weight=data_analysis$population_scaled[!is.na(data_analysis$population)])

#------------------------------------------------------------------------------#
### Table 6: Robustness of Results for President’s Party in State Elections ####
#------------------------------------------------------------------------------#

stargazer(state1,state2,state3,state4,state5,state6,state7,state8,state9,state10,state6_LDV,
          dep.var.labels = c("$\\Delta$ in Democratic Vote Share","Democratic Vote Share"),
          omit = "^pres_dem_control",
          order = c("wages_perworker_real_delta:pres_dem_control","wages_perworker_real_delta","partisanship_state_lagged"),
          covariate.labels = c("Change in logged wages $\\times$ Democratic president",
                               "Change in logged wages",
                               "Lagged Democratic voteshare"
          ),
          float=F,
          add.lines = list(c("FE for State-Year",            "X"," ","X","X"," ","X"," ","X","X"," ","X"),
                           c("FE for Year",                  " ","X"," "," ","X"," ","X"," "," ","X"," "),
                           c("FE for County",                "X","X"," ","X","X","X","X"," ","X","X","X"),
                           c("Unweighted, Pop $>$20,000",    "X","X","X"," "," ","X","X","X"," "," ","X"),
                           c("Weighted by Pop, All counties"," "," "," ","X","X"," "," "," ","X","X"," "),
                           " "),
          omit.stat = "ser",notes.append = F,
          notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
          out = "Tables/tab6.tex")

## Appendix D:Additional Specifications
state6_LDV <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control +partisanship_state_lag|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state6_LDV_nofe <- felm(partisanship_state ~ wages_perworker_real_delta* pres_dem_control +partisanship_state_lag|state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

stargazer(federal6_LDV,federal6_LDV_nofe,state6_LDV,state6_LDV_nofe)

#------------------------------------------------#
### Analysis of Accountability for Incumbents ####
#------------------------------------------------#

federal_pres <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* scale(house_dem_incumb) |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_pres <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_house <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control*house_dem_incumb|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## Just incumbency
data_analysis$incumbent <- data_analysis$pres_dem_control
president_incumbency <- felm(pres_demshare_delta ~ wages_perworker_real_delta* incumbent |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$house_dem_incumb
house_incumbency <- felm(house_demshare_delta ~ wages_perworker_real_delta*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$senate_dem_incumb
senate_incumbency <- felm(sen_demshare_delta ~ wages_perworker_real_delta* incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_incumbency$cse[c("incumbent")] <- NA # fixing incorrectly printed zeroes
data_analysis$incumbent <- data_analysis$gov_dem_control
gov_incumbency <- felm(gov_demshare_delta ~ wages_perworker_real_delta*  incumbent |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_incumbency$cse[c("incumbent")] <- NA # fixing incorrectly printed zeroes
data_analysis$incumbent <- data_analysis$sldl_dem_incumb
sldl_incumbency <- felm(sldl_demshare_delta ~ wages_perworker_real_delta*incumbent |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

#----------------------------------------------------------------#
### Appendix E: Simple model of accountability for incumbents ####
#----------------------------------------------------------------#

stargazer(senate_incumbency,house_incumbency,gov_incumbency,sldl_incumbency,
					dep.var.caption = "\\emph{Dependent Variable - Change in Vote Share for:}",
					dep.var.labels = c("Senate","House","Governor","State House"),
					omit = "^pres_dem_control",
					order = c("wages_perworker_real_delta:incumbent","wages_perworker_real_delta","incumbent"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic incumbent",
															 "Change in logged wages",
															 "Democratic incumbent"
					),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabE3.tex")

## Fully staturated model with incumbency and presidential control
data_analysis$incumbent <- data_analysis$pres_dem_control
president_incumbency2 <- felm(pres_demshare_delta ~ wages_perworker_real_delta* incumbent |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
president_incumbency2$cse[c("incumbent")] <- NA
data_analysis$incumbent <- data_analysis$house_dem_incumb
house_incumbency2 <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$senate_dem_incumb
senate_incumbency2 <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_incumbency2$cse[c("incumbent","pres_dem_control:incumbent")] <- NA
data_analysis$incumbent <- data_analysis$gov_dem_control
gov_incumbency2 <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control* incumbent |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_incumbency2$cse[c("incumbent","pres_dem_control:incumbent")] <- NA
data_analysis$incumbent <- data_analysis$sldl_dem_incumb
sldl_incumbency2 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*incumbent |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

#-------------------------------------------------------------------------#
### Appendix E: Fully saturated model of accountability for incumbents ####
#-------------------------------------------------------------------------#

stargazer(president_incumbency2,senate_incumbency2,house_incumbency2,gov_incumbency2,sldl_incumbency2,
					dep.var.caption = "\\emph{Dependent Variable - Change in Vote Share for:}",
					dep.var.labels = c("President","Senate","House","Governor","State House"),
					omit = "^pres_dem_control$",
					order = c("wages_perworker_real_delta:pres_dem_control:incumbent",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:incumbent",
										"wages_perworker_real_delta","incumbent",
										"pres_dem_control:incumbent"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres. $\\times$ Democratic incumbent",
															 "Change in logged wages $\\times$ Democratic president",
															 "Change in logged wages $\\times$ Democratic incumbent",
															 "Change in logged wages",
															 "Democratic incumbent",
															 "Democratic president $\\times$ Democratic incumbent"
					),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabE4.tex")

## Additive model of incumbency and presidential control
president_incumbency3 <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$house_dem_incumb
house_incumbency3 <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$senate_dem_incumb
senate_incumbency3 <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_incumbency3$cse[c("pres_dem_control","incumbent")] <- NA # adjusting incorrently printed zeroes
data_analysis$incumbent <- data_analysis$gov_dem_control
gov_incumbency3 <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* incumbent |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_incumbency3$cse[c("pres_dem_control","incumbent")] <- NA # adjusting incorrently printed zeroes
data_analysis$incumbent <- data_analysis$sldl_dem_incumb
sldl_incumbency3 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*incumbent |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

house_incumbency_nofe <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*house_dem_incumb|state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
gov_incumbency_nofe <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* gov_dem_control |state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])


#---------------------------------------------#
### Table 7: Accountability for Incumbents ####
#---------------------------------------------#

stargazer(president_incumbency3,senate_incumbency3,house_incumbency3,gov_incumbency3,sldl_incumbency3,
					dep.var.caption = "\\emph{Dependent Variable - Change in Vote Share for:}",
					dep.var.labels = c("President","Senate","House","Governor","State House"),
					omit = "^pres_dem_control",
					order = c("wages_perworker_real_delta:incumbent",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta",
										"incumbent"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic incumbent",
															 "Change in logged wages $\\times$ Democratic president",
															 "Change in logged wages",
															 "Democratic incumbent"
					),
					add.lines = list(c("FE for State-Year","X","X","X","X","X"),
													 c("FE for County","X","X","X","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tab7.tex")

## Additive model of icumbency and presidential control
president_incumbency4 <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$house_dem_incumb
house_incumbency4 <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$senate_dem_incumb
senate_incumbency4 <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*incumbent|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$gov_dem_control
gov_incumbency4 <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* incumbent |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$sldl_dem_incumb
sldl_incumbency4 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*incumbent |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
stargazer(president_incumbency4,senate_incumbency4,house_incumbency4,gov_incumbency4,sldl_incumbency4)

## possible configerations
## republican pres, republican incumbent
## republican pres, democratic incumbent
## democratic pres, republican incumbent
## democratic pres, democratic incumbent



#------------------------------------------#
### Changes in Accountability Over Time ####
#------------------------------------------#

data_analysis$period <- NA
data_analysis$period[data_analysis$year<1990] <- 0
data_analysis$period[data_analysis$year>1989]  <- 1

federal_nationalization <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year) |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_nationalization <- felm(partisanship_state_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year) +wages_perworker_real_delta* gov_dem_control*scale(year) |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## Table 8: 
stargazer(federal_nationalization, state_nationalization,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("Federal Elections","State/Local Elections"),
					omit = c("^pres_dem_control$","^scale\\(year\\)$",
									 "^pres_dem_control:scale\\(year\\)$",
									 "^scale\\(year\\):gov_dem_control$",
									 "^gov_dem_control$"),
					order = c("wages_perworker_real_delta:pres_dem_control:scale\\(year\\)",
										"wages_perworker_real_delta:scale\\(year\\):gov_dem_control",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:gov_dem_control",
										"wages_perworker_real_delta:scale\\(year\\)",
										"wages_perworker_real_delta"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres. $\\times$ year",
															 "Change in logged wages $\\times$ Democratic gov. $\\times$ year",
															 "Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages $\\times$ Democratic gov.",
															 "Change in logged wages $\\times$ year",
															 "Change in logged wages"
					),
					add.lines = list(c("FE for State-Year","X","X"),
													 c("FE for County","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tab8.tex"
					)


### Appendix G
president_nationalization <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year) |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$senate_dem_incumb
senate_nationalization <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year)+wages_perworker_real_delta* incumbent*scale(year) |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
senate_nationalization$cse[c("incumbent","scale(year):incumbent")] <- NA
data_analysis$incumbent <- data_analysis$house_dem_incumb
house_nationalization <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year)+wages_perworker_real_delta* incumbent*scale(year) |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$gov_dem_control
gov_nationalization <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year)+wages_perworker_real_delta* incumbent*scale(year)|fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_nationalization$cse[c("incumbent","scale(year):incumbent")] <- NA
data_analysis$incumbent <- data_analysis$sldl_dem_incumb
sldl_nationalization <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year)+wages_perworker_real_delta* incumbent*scale(year)|fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])

## Table G-7:
stargazer(president_nationalization ,senate_nationalization,house_nationalization,gov_nationalization,sldl_nationalization,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("President","Senate","House","Governor","State House"),
					omit = c("^pres_dem_control$",
									 "^scale\\(year\\)$",
									 "^pres_dem_control:scale\\(year\\)$"
									 ),
					order = c("wages_perworker_real_delta:pres_dem_control:scale\\(year\\)",
										"wages_perworker_real_delta:scale\\(year\\):incumbent",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:incumbent",
										"wages_perworker_real_delta:scale\\(year\\)",
										"wages_perworker_real_delta",
										"scale\\(year\\):incumbent",
										"incumbent"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres. $\\times$ year",
															 "Change in logged wages $\\times$ Democratic incumbent $\\times$ year",
															 "Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages $\\times$ Democratic incumbent",
															 "Change in logged wages $\\times$ year",
															 "Change in logged wages",
															 "Year $\\times$ Democratic incumbent",
															 "Democratic incumbent"
					),
					add.lines = list(c("FE for State-Year","X","X","X","X","X"),
													 c("FE for County","X","X","X","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabG7.tex")

## Models without incumbent control, only president control:
state_nationalization_incumbency <- felm(partisanship_state_delta ~ wages_perworker_real_delta* pres_dem_control*scale(year) +wages_perworker_real_delta* gov_dem_control*scale(year) |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

president_nationalization2 <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*period |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_nationalization2 <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*period+wages_perworker_real_delta* senate_dem_incumb*period |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
house_nationalization2 <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*period+wages_perworker_real_delta* house_dem_incumb*period |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_nationalization2 <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*period+wages_perworker_real_delta* gov_dem_control*period|fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
sldl_nationalization2 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*period+wages_perworker_real_delta* sldl_dem_incumb*period|fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
stargazer(president_nationalization2 ,senate_nationalization2,house_nationalization2,gov_nationalization2)

#----------------------------------------------------#
### Appendix H: Pre- vs. Post-1990 Accountability ####
#----------------------------------------------------#

pres_pre1990 <- felm(pres_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										 | fips + state_year |0| fips, 
										 data=filter(data_analysis, population_2010 > 20000 & year<1990))
sen_pre1990 <- felm(sen_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										| fips + state_year |0| fips, 
										data=filter(data_analysis, population_2010 > 20000 & year<1990))
house_pre1990 <- felm(house_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											| fips + state_year |0| fips, 
											data=filter(data_analysis, population_2010 > 20000 & year<1990))
federal_pre1990 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta*pres_dem_control
												| fips + state_year |0| fips, 
												data=filter(data_analysis, population_2010 > 20000 & year<1990))
gov_pre1990 <- felm(gov_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										| fips + state_year |0| fips, 
										data=filter(data_analysis, population_2010 > 20000 & year<1990))
stateexec_pre1990 <- felm(stateoffice_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
													| fips + state_year |0| fips, 
													data=filter(data_analysis, population_2010 > 20000 & year<1990))
sldl_pre1990 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										 | fips + state_year |0| fips, 
										 data=filter(data_analysis, population_2010 > 20000 & year<1990))
sldu_pre1990 <- felm(sldu_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										 | fips + state_year |0| fips, 
										 data=filter(data_analysis, population_2010 > 20000 & year<1990))
state_pre1990 <- felm(partisanship_state_delta ~ wages_perworker_real_delta*pres_dem_control
											| fips + state_year |0| fips, 
											data=filter(data_analysis, population_2010 > 20000 & year<1990))
county_pre1990 <- felm(countyleg_avg_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											 | fips + state_year |0| fips, 
											 data=filter(data_analysis, population_2010 > 20000 & year<1990))
mayor_pre1990 <- felm(mayor_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											| fips + state_year |0| fips, 
											data=filter(data_analysis, population_2010 > 20000 & year<1990))

## Post:
pres_post1990 <- felm(pres_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											| fips + state_year |0| fips, 
											data=filter(data_analysis, population_2010 > 20000 & year>=1990))
sen_post1990 <- felm(sen_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										 | fips + state_year |0| fips, 
										 data=filter(data_analysis, population_2010 > 20000 & year>=1990))
house_post1990 <- felm(house_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											 | fips + state_year |0| fips, 
											 data=filter(data_analysis, population_2010 > 20000 & year>=1990))
federal_post1990 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta*pres_dem_control
												 | fips + state_year |0| fips, 
												 data=filter(data_analysis, population_2010 > 20000 & year>=1990))
gov_post1990 <- felm(gov_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
										 | fips + state_year |0| fips, 
										 data=filter(data_analysis, population_2010 > 20000 & year>=1990))
stateexec_post1990 <- felm(stateoffice_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
													 | fips + state_year |0| fips, 
													 data=filter(data_analysis, population_2010 > 20000 & year>=1990))
sldl_post1990 <- felm(sldl_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											| fips + state_year |0| fips, 
											data=filter(data_analysis, population_2010 > 20000 & year>=1990))
sldu_post1990 <- felm(sldu_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											| fips + state_year |0| fips, 
											data=filter(data_analysis, population_2010 > 20000 & year>=1990))
state_post1990 <- felm(partisanship_state_delta ~ wages_perworker_real_delta*pres_dem_control
											 | fips + state_year |0| fips, 
											 data=filter(data_analysis, population_2010 > 20000 & year>=1990))
county_post1990 <- felm(countyleg_avg_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
												| fips + state_year |0| fips, 
												data=filter(data_analysis, population_2010 > 20000 & year>=1990))
mayor_post1990 <- felm(mayor_demshare_delta ~ wages_perworker_real_delta*pres_dem_control
											 | fips + state_year |0| fips, 
											 data=filter(data_analysis, population_2010 > 20000 & year>=1990))

## Table H-8 ##
stargazer(federal_pre1990, state_pre1990,federal_post1990, state_post1990,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("Federal (pre-1990)","State (pre-1990)","Federal (post-1990)","State (post-1990)"),
					omit = c("^pres_dem_control$"
					),
					order = c("wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages"
					),
					add.lines = list(c("FE for State-Year","X","X","X","X"),
													 c("FE for County","X","X","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabH8.tex")


#--------------------------------------------#
### Analysis for Media and Accountability ####
#--------------------------------------------#

data_analysis$state_year_newspaper <- paste(data_analysis$state_year,data_analysis$county_newspaper, sep="-")
data_analysis$state_year_newspaper2 <- paste(data_analysis$state_year,data_analysis$dailypaper, sep="-")

data_analysis$dailypaper2<-data_analysis$dailypaper
data_analysis$dailypaper2[data_analysis$county_newspaper==1]<-1

federal_media <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper|fips+state_year_newspaper|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_media <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control*county_newspaper|fips+state_year_newspaper|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])


federal_media2 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control*dailypaper|fips+state_year_newspaper2|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_media2 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control*dailypaper|fips+state_year_newspaper2|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])


president_media <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper|fips+state_year_newspaper|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_president_media <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
house_president_media <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])

president_media <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper|fips+state_year_newspaper|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
president_media$cse["pres_dem_control:county_newspaper"] <- NA
data_analysis$incumbent <- data_analysis$senate_dem_incumb
senate_president_media <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper+wages_perworker_real_delta* incumbent*county_newspaper |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_president_media$cse["incumbent"] <- NA
data_analysis$incumbent <- data_analysis$house_dem_incumb
house_president_media <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper+wages_perworker_real_delta* incumbent*county_newspaper |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
data_analysis$incumbent <- data_analysis$gov_dem_control
gov_president_media <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper+wages_perworker_real_delta* incumbent*county_newspaper |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_president_media$cse["incumbent"] <- NA
data_analysis$incumbent <- data_analysis$sldl_dem_incumb
sldl_president_media <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper+wages_perworker_real_delta* incumbent*county_newspaper |fips+state_year|0|fips,  data=data_analysis[  data_analysis$population_2010 > 20000,])
cities_president_media <- felm(mayor_demshare_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper+wages_perworker_real_delta*mayor_dem_control*county_newspaper+wages_perworker_real_delta* gov_dem_control*county_newspaper |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
counties_president_media <- felm(countyleg_avg_demshare_delta ~ wages_perworker_real_delta * pres_dem_control*county_newspaper+wages_perworker_real_delta* county_dem_control*county_newspaper+wages_perworker_real_delta* gov_dem_control*county_newspaper|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])


#----------------------------------------#
### Table 9: Media and Accountability ####
#----------------------------------------#

stargazer(federal_media, state_media,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("Federal Elections","State/Local Elections"),
					omit = c("^pres_dem_control$",
									 "^county_newspaper$",
									 "^pres_dem_control:county_newspaper$"
									 ),
					order = c("wages_perworker_real_delta:pres_dem_control:county_newspaper",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:county_newspaper",
										"wages_perworker_real_delta"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres. $\\times$ newspaper",
															 "Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages $\\times$ newspaper",
															 "Change in logged wages"
					),
					add.lines = list(c("FE for State-Year","X","X"),
													 c("FE for County","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tab9.tex")

#---------------------------------------------------#
## Table I-9: Media and Accountability by office ####
#---------------------------------------------------#

stargazer(president_media,senate_president_media,house_president_media,gov_president_media,sldl_president_media,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("President","Senate","House","Governor","State House"),
					omit = c("^pres_dem_control$",
									 "^county_newspaper$"
					),
					order = c("wages_perworker_real_delta:pres_dem_control:county_newspaper",
										"wages_perworker_real_delta:county_newspaper:incumbent",
										"^wages_perworker_real_delta$",
										"^incumbent$",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:county_newspaper",
										"pres_dem_control:county_newspaper",
										"wages_perworker_real_delta:incumbent",
										"county_newspaper:incumbent"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres. $\\times$ newspaper",
															 "Change in logged wages $\\times$Democratic incumbent $\\times$ newspaper",
															 "Change in logged wages",
															 "Democratic incumbent",
															 "Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages $\\times$ newspaper",
															 "Democratic pres. $\\times$ newspaper",
															 "Change in logged wages$\\times$Democratic incumbent",
															 "Democratic incumbent $\\times$ newspaper"
					),
					add.lines = list(c("FE for State-Year","X","X","X","X","X"),
													 c("FE for County","X","X","X","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabI9.tex")

#----------------------------------------------#
## Appendix J: Alternative Measure of Media ####
#----------------------------------------------#
data_analysis$county_newspaper_same <- data_analysis$county_newspaper
federal_media <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper_same|fips+state_year_newspaper|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_media <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control*county_newspaper_same|fips+state_year_newspaper|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

data_analysis$county_newspaper_same <- data_analysis$dailypaper
federal_media2 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control*county_newspaper_same|fips+state_year_newspaper2|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_media2 <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control*county_newspaper_same|fips+state_year_newspaper2|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

stargazer(federal_media, state_media,federal_media2, state_media2,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("Federal (XS)","State (XS)","Federal (Panel)","State (Panel)"),
					omit = c("^pres_dem_control$",
									 "^county_newspaper_same$",
									 "^pres_dem_control:county_newspaper_same$"
					),
					order = c("wages_perworker_real_delta:pres_dem_control:county_newspaper_same",
										"wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:county_newspaper_same",
										"wages_perworker_real_delta"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres. $\\times$ newspaper",
															 "Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages $\\times$ newspaper",
															 "Change in logged wages"
					),
					add.lines = list(c("FE for State-Year-Newspaper","X","X","X","X"),
													 c("FE for County","X","X","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabJ10.tex")



## General ECM
federal0 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal1 <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control+partisanship_federal_lag |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal2 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_lag1* pres_dem_control+partisanship_federal_lag |fips+year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])


#---------------------------------------------------------------------------------------#
### Appendix F: Accountability for Partisan Control of Government outside Presidency ####
#---------------------------------------------------------------------------------------#

federal_localoffice <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* house_party +wages_perworker_real_delta* senate_party |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_localoffice <- felm(partisanship_state_delta ~ wages_perworker_real_delta* pres_dem_control +wages_perworker_real_delta* gov_dem_control|fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
state_localofficeb <- felm(partisanship_state_delta2 ~ wages_perworker_real_delta* pres_dem_control +wages_perworker_real_delta* gov_dem_control|fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])


president <- felm(pres_demshare_delta ~ wages_perworker_real_delta* pres_dem_control +wages_perworker_real_delta* senate_party+wages_perworker_real_delta* house_party|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
senate_localoffice <- felm(sen_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* house_party +wages_perworker_real_delta* senate_party |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
house_localoffice <- felm(house_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* house_party+wages_perworker_real_delta* senate_party |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
gov_localoffice <- felm(gov_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* gov_dem_control |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
sldl_localoffice <- felm(sldl_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* gov_dem_control |fips+state_year|0|fips,  data=data_analysis[ data_analysis$population_2010 > 20000,])
cities_localoffice <- felm(mayor_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta*mayor_dem_control+wages_perworker_real_delta* gov_dem_control |fips+state_year|0|fips,  data=data_analysis[data_analysis$population_2010 > 20000,])
counties_localoffice <- felm(countyleg_avg_demshare_delta_alldistricts ~ wages_perworker_real_delta * pres_dem_control+wages_perworker_real_delta* county_dem_control|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
counties_localoffice3 <- felm(countyleg_avg_demshare_delta_alldistricts ~ wages_perworker_real_delta * pres_dem_control+wages_perworker_real_delta* county_dem_control+wages_perworker_real_delta* gov_dem_control|fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
stateoffice_localoffice <- felm(stateoffice_demshare_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta* gov_dem_control  |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

#-------------------------------------------------------------------------#
## Table F-5: Accountability for Partisan Control in Federal Elections ####
#-------------------------------------------------------------------------#

stargazer(president, senate_localoffice, house_localoffice, federal_localoffice,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Vote Share for:}",
					dep.var.labels = c("President","Senate","House","Federal Average"),
					omit = c("^pres_dem_control$","^senate_party$","^house_party$"),
					order = c("wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:senate_party",
										"wages_perworker_real_delta:house_party",
										"wages_perworker_real_delta"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic pres.",
															 "Change in logged wages $\\times$ Democratic Senate",
															 "Change in logged wages $\\times$ Democratic House",
															 "Change in logged wages"
					),
					float=F,
					add.lines = list(c("FE for State-Year","X","X","X","X"),
													 c("FE for County","X","X","X","X")),
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabF5.tex")

#---------------------------------------------------------------------------------#
## Table F-6: Accountability for Partisan Control in State and Local Elections ####
#---------------------------------------------------------------------------------#

stargazer(gov_localoffice,stateoffice_localoffice, sldl_localoffice,counties_localoffice,state_localofficeb,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Vote Share for:}",
					dep.var.labels = c("Governor","Downballot State Offices","State House","County Legislature","State/Local Average"),
					omit = c("^pres_dem_control$","^gov_dem_control$","^county_dem_control$"),
					order = c("wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta:gov_dem_control",
										"wages_perworker_real_delta:county_dem_control",
										"wages_perworker_real_delta"),
					covariate.labels = c("Change in logged wages $\\times$ Democratic Pres.",
															 "Change in logged wages $\\times$ Democratic Gov.",
															 "Change in logged wages $\\times$ Democratic Leg.",
															 "Change in logged wages"
					),
					float=F,
					add.lines = list(c("FE for State-Year","X","X","X","X","X"),
													 c("FE for County","X","X","X","X","X")),
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabF6.tex")



#--------------------------------------#
### Additional checks for reviewers ####
#--------------------------------------#

### deltas, FE versus no FE
federal_check_fe <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_check_nofe <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

federal_check_fe_nocluster <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_check_nofe_nocluster <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |state_year|0, data=data_analysis[data_analysis$population_2010 > 20000,])

summary(federal_check_fe)
summary(federal_check_fe_nocluster)

summary(federal_check_nofe)
summary(federal_check_nofe_nocluster)
f
### levels, FE versus no FE
federal_check_fe <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_check_nofe <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control |state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

federal_check_fe_nocluster <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control |fips+state_year|0, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_check_nofe_nocluster <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control |state_year|0, data=data_analysis[data_analysis$population_2010 > 20000,])

summary(federal_check_fe)
summary(federal_check_fe_nocluster)

summary(federal_check_nofe)
summary(federal_check_nofe_nocluster)

#-------------------------------------------------------------#
### Placebo checks for parallel trends, June 2019 addition ####
#-------------------------------------------------------------#

## main result - deltas
federal_check1 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## levels
federal_check2 <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_check3 <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control+partisanship_federal_lag |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

## placebo for main result - deltas
federal_placebo1 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lead1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
## placebo using levels
federal_placebo2 <- felm(partisanship_federal ~ wages_perworker_real_lead1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_placebo3 <- felm(partisanship_federal ~ wages_perworker_real_lead1* pres_dem_control +partisanship_federal_lag |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

summary(federal_check1)
summary(federal_placebo1)

summary(federal_check2)
summary(federal_placebo2)

summary(federal_check3)
summary(federal_placebo3)

### expansion of placebo checks to evaluate validity of parallel trends
federal_trends1 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lag3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends2 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lag2* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends3 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lag1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends4 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends5 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lead1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends6 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lead2* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends7 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lead3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends8 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lead4* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends9 <- felm(partisanship_federal_delta ~ wages_perworker_real_delta_lead5* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

stargazer(federal_trends1,federal_trends2,federal_trends3,federal_trends4,federal_trends5,federal_trends6,federal_trends7,federal_trends8,federal_trends9)


### expansion of placebo checks to evaluate validity of parallel trends
federal_trends1 <- felm(partisanship_federal ~ wages_perworker_real_lag3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends2 <- felm(partisanship_federal ~ wages_perworker_real_lag2* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends3 <- felm(partisanship_federal ~ wages_perworker_real_lag1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends4 <- felm(partisanship_federal ~ wages_perworker_real_log* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends5 <- felm(partisanship_federal ~ wages_perworker_real_lead1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends6 <- felm(partisanship_federal ~ wages_perworker_real_lead2* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends7 <- felm(partisanship_federal ~ wages_perworker_real_lead3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends8 <- felm(partisanship_federal ~ wages_perworker_real_lead4* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends9 <- felm(partisanship_federal ~ wages_perworker_real_lead5* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])



### expansion of placebo checks to evaluate validity of parallel trends
federal_trends1 <- felm(partisanship_federal ~ wages_perworker_real_delta_lag3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends2 <- felm(partisanship_federal ~ wages_perworker_real_delta_lag2* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends3 <- felm(partisanship_federal ~ wages_perworker_real_delta_lag1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends4 <- felm(partisanship_federal ~ wages_perworker_real_delta* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends5 <- felm(partisanship_federal ~ wages_perworker_real_delta_lead1* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends6 <- felm(partisanship_federal ~ wages_perworker_real_delta_lead2* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends7 <- felm(partisanship_federal ~ wages_perworker_real_delta_lead3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends8 <- felm(partisanship_federal ~ wages_perworker_real_delta_lead4* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
federal_trends9 <- felm(partisanship_federal ~ wages_perworker_real_delta_lead5* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])







##---------------------------------------------------------------##
#### Appendix C: Effect of Economy Over Entire Electoral Cycle ####
##---------------------------------------------------------------##
### Lagged economic growth, June 2019 addition
federal_electoral_cycle <- felm(partisanship_federal_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta_lag1* pres_dem_control +wages_perworker_real_delta_lag2* pres_dem_control +wages_perworker_real_delta_lag3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])
state_electoral_cycle <- felm(partisanship_state_delta ~ wages_perworker_real_delta* pres_dem_control+wages_perworker_real_delta_lag1* pres_dem_control +wages_perworker_real_delta_lag2* pres_dem_control +wages_perworker_real_delta_lag3* pres_dem_control |fips+state_year|0|fips, data=data_analysis[data_analysis$population_2010 > 20000,])

stargazer(federal_electoral_cycle, state_electoral_cycle,
					dep.var.caption = "\\emph{Dependent Variable - $\\Delta$ in Democratic Vote Share for:}",
					dep.var.labels = c("Federal Average","State Average"),
					omit = c("^pres_dem_control$"
					),
					order = c("wages_perworker_real_delta:pres_dem_control",
										"wages_perworker_real_delta_lag1:pres_dem_control",
										"wages_perworker_real_delta_lag2:pres_dem_control",
										"wages_perworker_real_delta_lag3:pres_dem_control",
										"wages_perworker_real_delta",
										"wages_perworker_real_delta_lag1",
										"wages_perworker_real_delta_lag2",
										"wages_perworker_real_delta_lag3"
					),
					covariate.labels = c("Change in logged wages $\\times$ Democratic president",
															 "Change in logged wages (t-1) $\\times$ Democratic president",
															 "Change in logged wages (t-2) $\\times$ Democratic president",
															 "Change in logged wages (t-3) $\\times$ Democratic president",
															 "Change in logged wages",
															 "Change in logged wages (t-1)",
															 "Change in logged wages (t-2)",
															 "Change in logged wages (t-3)"
					),
					add.lines = list(c("FE for State-Year","X","X"),
													 c("FE for County","X","X")),
					float=F,
					omit.stat = "ser",notes.append = F,
					notes = "Standard errors clustered by county. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01",notes.align = "l",
					out = "Tables/tabC1.tex")


